package com.theme.passport.controller.app;

import com.theme.common.base.DateHandler;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.SysUserEntity;
import com.theme.passport.entity.constant.DownloadTypeEnum;
import com.theme.passport.entity.constant.ThemeStatusEnum;
import com.theme.passport.entity.theme.WallpaperEntity;
import com.theme.passport.service.SysUserService;
import com.theme.passport.service.theme.DownloadHistoryService;
import com.theme.passport.service.theme.WallpaperClassifyService;
import com.theme.passport.service.theme.WallpaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: 可乐加糖
 * @create: 2022-06-28 16:30
 * @Description: 壁纸管理
 */
@RestController
@RequestMapping("app/wallpapers")
@Api("壁纸管理相关接口")
public class WallpapersController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private WallpaperService wallpaperService;

    @Autowired
    private WallpaperClassifyService wallpaperClassifyService;

    @Autowired
    private DownloadHistoryService downloadHistoryService;

    @GetMapping("classify")
    @ApiOperation("获取壁纸分类接口")
    public R classify(@RequestParam(value = "keyword", required = false) String keyword,
                      @RequestParam(value = "page", defaultValue = "1") String page,
                      @RequestParam(value = "limit", defaultValue = "10") String limit) {
        Map<String, Object> params = new HashMap<>();
        params.put("key", keyword);
        params.put("page", page);
        params.put("limit", limit);
        PageUtils pages = wallpaperClassifyService.queryPage(params);
        return R.ok().put("page", pages);
    }

    @GetMapping("list")
    @ApiOperation("获取壁纸列表")
    public R list(@RequestParam(value = "keyword", required = false) String keyword,
                  @RequestParam(value = "free", required = false) String free,
                  @RequestParam(value = "classifyId", required = false) String classifyId,
                  @RequestParam(value = "page", defaultValue = "1") String page,
                  @RequestParam(value = "limit", defaultValue = "10") String limit) {
        Integer status = ThemeStatusEnum.NORMAL.getValue();
        Map<String, Object> params = new HashMap<>();
        params.put("status", String.valueOf(status));
        params.put("key", keyword);
        params.put("free", free);
        params.put("classifyId", classifyId);
        params.put("page", page);
        params.put("limit", limit);
        PageUtils pages = wallpaperService.queryPage(params);
        List<WallpaperEntity> list = (List<WallpaperEntity>) pages.getList();
        list.forEach(wallpaper ->{
            //SysUserEntity byId = sysUserService.getById(wallpaper.getUserId());
            //wallpaper.setUseNickname(byId.getNickname());
            String label = wallpaper.getLabel();
            if (label != null){
                String[] split = label.split(",");
                wallpaper.setLabels(Arrays.asList(split));
            }
        });
        pages.setList(list);
        return R.ok().put("page", pages);
    }

    @GetMapping("featured")
    @ApiOperation("查询精选|福利壁纸接口[free表示1收费0免费]")
    public R featured(@RequestParam(value = "keyword", required = false) String keyword,
                      @RequestParam(value = "classifyId", required = false) String classifyId,
                      @RequestParam(value = "free",  defaultValue = "1") String free,
                      @RequestParam(value = "page", defaultValue = "1") String page,
                      @RequestParam(value = "limit", defaultValue = "10") String limit) {
        return this.list(keyword, classifyId, free, page, limit);
    }

    @GetMapping("/info")
    @ApiOperation("查询壁纸详情")
    public R info(@RequestParam("wallpaperId") Long wallpaperId) throws Exception {
        WallpaperEntity wallpaper = wallpaperService.getById(wallpaperId);
        if(wallpaper == null){
            return R.error("该壁纸不存在或已下架!!!");
        }
        SysUserEntity byId = sysUserService.getById(wallpaper.getUserId());
        wallpaper.setUseNickname(byId.getNickname());
        String label = wallpaper.getLabel();
        if (label != null){
            String[] split = label.split(",");
            wallpaper.setLabels(Arrays.asList(split));
        }
        return R.ok().put("wallpaper", wallpaper);
    }

    @GetMapping("hotBakup")
    @ApiOperation("获取热门壁纸[前七天下载量最高的]")
    public R hot(@RequestParam(value = "page", defaultValue = "1") String page,
                 @RequestParam(value = "limit", defaultValue = "10") String limit) {
        //1、根据时间查询昨天24小时（一天）
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = DateHandler.addDateDays(new Date(),-7);
        Date endDate = DateHandler.addDateDays(new Date(),-1);
        Date startTime = DateHandler.getStartTime(startDate);
        Date endTime = DateHandler.getEndTime(endDate);
        Map<String, Object> hotParams = new HashMap<>();
        hotParams.put("type", DownloadTypeEnum.WALLPAPER.getValue());
        hotParams.put("startTime", formatter.format(startTime));
        hotParams.put("endTime", formatter.format(endTime));
        //2、得的主题id
        List<Long> wallpapersIds = downloadHistoryService.queryHotDownloadIds(hotParams);
        //3、根据主题id查询
        Map<String, Object> params = new HashMap<>(6);
        params.put("page", page);
        params.put("limit", limit);
        //没有最新下的话数据正常显示几个
        if (wallpapersIds == null){
            PageUtils pages =wallpaperService.queryPage(params);
            List<WallpaperEntity> list = (List<WallpaperEntity>) pages.getList();
            list.forEach(wallpaper ->{
                String label = wallpaper.getLabel();
                if (label != null){
                    String[] split = label.split(",");
                    wallpaper.setLabels(Arrays.asList(split));
                }
            });
            pages.setList(list);
            return R.ok().put("page", pages);
        }
        PageUtils pages =wallpaperService.queryWallpaperByIds(params,wallpapersIds);
        List<WallpaperEntity> list = (List<WallpaperEntity>) pages.getList();
        list.forEach(wallpaper ->{
            String label = wallpaper.getLabel();
            if (label != null){
                String[] split = label.split(",");
                wallpaper.setLabels(Arrays.asList(split));
            }
        });
        pages.setList(list);
        return R.ok().put("page", pages);
    }

}
